Fix for custom distinct
handling in laterals
#174
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi folks 👋🏻
I noticed that I wasn't able to add custom
distinct
clause to my has_many assoc query - it was raising "multiple distinct clauses not allowed" error. Then I looked at source code and there was a bypass clause for custom distinct already, but it was ignored. It looks like a bug, here's an explanation:Ecto represents distinct clauses in a query as a
%Ecto.Query.QueryExpr{}
struct. In Elixir, guards pass only when the evaluated condition is true but since the distinct struct is not explicitly true, the guard fails to pass.This update reverses the guard logic so that it passes as long as the distinct value is not nil.
I wasn't sure if I should use
is_nil
vs matching on%QueryExpr{}
struct explicitly - that struct is part of Ecto's internals, but on the other hand Dataloader already matches on different structs like assocs representation. Let me know what you think.Test was passing because more general clause with limit and order_by keys was located before one that adds
distinct
, changing the order resulted in: